package com.py.korona.ui.activities.room;

import java.io.ByteArrayInputStream;
import java.lang.ref.WeakReference;
import java.util.Arrays;
import java.util.HashMap;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;

public class RoomListBinder implements ViewBinder {
	private HashMap<String, Bitmap> cache;

	public RoomListBinder() {
		super();
	}

	public RoomListBinder(HashMap<String, Bitmap> cache) {
		super();
		this.cache = cache;
	}

	@Override
	public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
		boolean binded = false;
		if (view instanceof RatingBar) {
			/*
			 * rating bar
			 */
			float value = cursor.getFloat(columnIndex);
			RatingBar ratingBar = (RatingBar) view;
			ratingBar.setRating(value);
			binded = true;
		} else if (view instanceof ImageView) {
			/*
			 * small image
			 */
			ImageView imageView = (ImageView) view;
			String id = cursor.getString(cursor.getColumnIndex("_id"));
			Bitmap bitmap = this.fetchBitmapFromCache(id);
			if (null != bitmap) {
				imageView.setImageBitmap(bitmap);
			} else {
				byte[] imageData = cursor.getBlob(columnIndex);
				if (null == imageData) {
					imageView.setImageResource(android.R.color.transparent);
				} else {
					BitmapDownloadTask task = new BitmapDownloadTask(imageView,
							id);
					task.execute(imageData);
				}
			}
			binded = true;
		}
		return binded;
	}

	private Bitmap fetchBitmapFromCache(final String id) {
		synchronized (cache) {
			final Bitmap result = cache.get(id);
			if (result != null) {
				cache.remove(id);
				cache.put(id, result);
			}
			return result;
		}
	}

	private void addBitmapToCache(String id, Bitmap bitmap) {
		if (null != bitmap) {
			synchronized (cache) {
				cache.put(id, bitmap);
			}
		}
	}

	private class BitmapDownloadTask extends AsyncTask<byte[], Void, Bitmap> {
		private final WeakReference<ImageView> imageViewReference;
		private byte[] data;
		private String id;

		public BitmapDownloadTask(ImageView p_imageView, String id) {
			this.imageViewReference = new WeakReference<ImageView>(p_imageView);
			this.id = id;
		}

		@Override
		protected Bitmap doInBackground(byte[]... arg0) {
			if (null == arg0[0]) {
				return null;
			}
			this.data = Arrays.copyOf(arg0[0], arg0[0].length);
			if (null == this.data) {
				return null;
			}
			return BitmapFactory.decodeStream(new ByteArrayInputStream(
					this.data));
		}

		@Override
		protected void onPostExecute(Bitmap result) {
			if (isCancelled()) {
				result = null;
			}
			addBitmapToCache(this.id, result);
			if (null != imageViewReference) {
				ImageView imageView = imageViewReference.get();
				if (null != imageView) {
					imageView.setImageBitmap(result);
				}
			}
		}
	}
}
